package com.huiminpay.transaction.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.huiminpay.common.cache.util.QRCodeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@Slf4j
public class PayTestController {
    String APP_ID = "9021000139658847";
    String APP_PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDP96HonbwK1eWN0h6Cm/yM25mOUCUGIgy3sY285+DsvSGef/JxVwZQ9KRws987kTXLci9k8d9vMOHHJtpX8Vbx3MvTvEkxovwgZlWYYMXSQrY5lg3WnmheAH3zTQtwoL3pH98Om8fNY3pBo1tsAoQ2wTDHAky/O44Lu0js5Osaqi89jlyZdROIvGtKgjdAyERdRMrRqBsDTl0a/F50VdVIwtlVh6+D2mO5kmjVG+eZyRLjRih0LzTdltuu2PDMtzfkCRro/pBPMghNYzKzjNw+pX+ugP1yPZ8MQBs/ssbMssrVNUIoZorJUCS4hxVHw5NPjTnZqRISDq8ytqOOIqPHAgMBAAECggEASnyGSBueyz67PmWAugUGmqjYwekgqaauaWi1vHBM1l4TWYK4IjIqN/q3nwo9rUcgPmMCaKRdA6U9aKgY0iDFpFERzB+HE7ktlqkwhSg8EkgEnnGJOVJ5TRneArOYollgS8w3hzLyiDTWCAZray8njbj6MH05YdEB2l/VCcwgDOc787rdgaUM2T/y6x8CgQWQNcYJYW4JSX/YoNwc+hxc2L5k5Kv9EIenc8HCod4meff8lZljqzZmtZkTS66FkLf0anWfgtVWZ/Yfb2pWm9ucW0u0T5RHfZNrzcLMov5c5N6Q04RluWDt2VU41klgKslm32kynaAZVBG7FZRbUR45wQKBgQD1fuFj8YvDOIi2OBbvNzgTN+Dm5cYlproqoIDxGWCyUWaRk54e9vrz27C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sD6NbAbPctqjSZIgNb3DzR7Pi24ua9z/yS/KhuO8bvdkrFpdRDSXZ7Ewmf538M6aPTJavFK9aKfnFnQfNPNk00baLKPhHCEKkdP91NgmL7vOi8hWAaH5ReEFec34KLUJMZDapEZuc0AgdEHKtFTtwIDAQAB";
    String CHARSET = "UTF-8";
    String serverUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";


    @RequestMapping("/alipayTest")
    public void alipayTest(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取初始化的AlipayClient
        DefaultAlipayClient alipayClient = new DefaultAlipayClient(serverUrl, APP_ID, APP_PRIVATE_KEY, "json", CHARSET, ALIPAY_PUBLIC_KEY, "RSA2");
        // 创建API对应的request
        AlipayTradeWapPayRequest payRequest = new AlipayTradeWapPayRequest();
        // 封装参数
        payRequest.setBizContent("{" +
                " \"out_trade_no\":\"20150320010101123\"," +
                " \"total_amount\":\"10.00\"," +
                " \"subject\":\"后端炒米粉\"," +
                " \"product_code\":\"QUICK_WAP_PAY\"" +
                " }");
        // 执行API
        String form = "";
        try {
            form = alipayClient.pageExecute(payRequest).getBody();
        } catch (AlipayApiException e) {
            throw new RuntimeException(e);
        }
        // 输出表单字符串
        log.info("form:{}", form);

        response.setContentType("text/html;charset=" + CHARSET);
        response.getWriter().write(form); // 直接将完整的表单html输出到页面
        response.getWriter().flush();
        response.getWriter().close();
    }

    public static void main(String[] args) throws IOException {
        QRCodeUtil qrCodeUtil = new QRCodeUtil();
        System.out.println(qrCodeUtil.createQRCode("http://172.16.1.103:56050/transaction/alipayTest",
                200, 200));
    }
}